package java_thinking.unit_15._15_8;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description :  修改练习21，使得工厂对象是由一个Map而不是Class<?>持有的
 * @author: cww
 * @DateTime: 2019-04-05 15:09
 */

public class E24 {
    Map<String,FactoryI<?>> factoryIMap = new HashMap<>();
    public Object createNew(String name,int arg){
        try {
            FactoryI<?> factoryI = factoryIMap.get(name);
            return factoryI.create(arg);
        } catch (Exception e) {
            System.out.println("Not a registered factoryname: " + name);;
        }
        return null;
    }
    public void addFactory(String factoryName,FactoryI<?> factoryI){
        factoryIMap.put(factoryName,factoryI);
    }
}

class FactoryCapture {
    public static void main(String[] args) {
        E24 e24 = new E24();
        e24.addFactory("Integer",new IntegerFactory());
        e24.addFactory("Widget",new Wigdet.Factory());
        System.out.println(e24.createNew("Integer",3));
        System.out.println(e24.createNew("Widget", 2));
        e24.createNew("Product", 3);
    }
}
